/*****************************************************************
*This class is derived from CHttpReq ,to provide more connection rate limit control
*
*The public interface is the same as CHttpReq, while CHttpReq is just a private base
*
*This is the real reqest interface you should use.
*
*By GuiQing, (c)2010        Specialisation Level : sina API
******************************************************************/

/** To get 200 followers per request need login ! Or it return 20 followers by default***/

#ifndef CHTTPREQLIMITED_H_INCLUDED
#define CHTTPREQLIMITED_H_INCLUDED

#include "GlobalConfig.h"

#include "CHttpReq.h"

#ifdef RATE_LIMIT_DB
    #include "CQuery.h"
#endif

#include <stdexcept>

#define RATE_LIMIT_ERROR "系统繁忙，您先歇会，喝口水"
#define SINA_RATE_LIMIT_API "http://api.t.sina.com.cn/account/rate_limit_status.xml"
#ifndef SINA_RATE_LIMIT_NUM
    #define SINA_RATE_LIMIT_NUM ((long) 1000)
#endif
#define SYNC_RATE_LIMIT_ERROR "无法连接"

class TiXmlDocument;
class TiXmlNode;
class TiXmlElement;

class CHttpReqLimited : public CHttpReq
{
    typedef TiXmlDocument DOM;
    typedef TiXmlNode Node;
    typedef TiXmlElement Element;

    private:

        #ifdef RATE_LIMIT_DB
            static CQuery dbQuery_for_rateLimit;
        #endif
        static int rateUsed;
        //static int rateRemaning;
        static long resetSec;
        static wxString nextResetTime;
        static wxString lastResetTime;



        //hourly statistics from Sina API(http://api.t.sina.com.cn/account/rate_limit_status.xml?source=)

        //CHttpReqLimited(const CHttpReqLimited& obj);
        bool isRateLimited();
        bool currentRateReq();

    public:

        CHttpReqLimited (const bool auth=true, const int retrySec_IN=HTTP_RETRY_WAIT_SEC, const int timeOutSec_IN=HTTP_TIME_OUT_SEC);

        virtual ~CHttpReqLimited ();

        //IMPORTANT : if you don't provide cursor, then you can't get <next_cursor>
        //                   And if you provide cursor=0 , then http req loop will exit
        //                   So , by default, cursor init to -1 is critical !
        bool startReqLimited(const wxString& urlStr_IN, long count=0, long cursor=-1,const wxString& postBuf="");
};

#endif // CHTTPREQLIMITED_H_INCLUDED
